﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Helper;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace KouDai.Phone.ViewModels
{
    public class HotViewModel : KouDaiScreen
    {
        #region Fields
        private readonly INavigationService _navigationService;
        private ObservableCollection<HotEntity> _hotItems = new ObservableCollection<HotEntity>();
        #endregion

        #region Constructor
        public HotViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public HotViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
        public ObservableCollection<HotEntity> HotItems
        {
            get { return _hotItems; }
            set
            {
                if (value != _hotItems)
                {
                    _hotItems = value;
                    this.NotifyOfPropertyChange(() => this.HotItems);
                }
            }
        }
        #endregion

        #region Override

        public override void LoadDesignData()
        {
            List<HotEntity> hotList = new List<HotEntity>();
            HotEntity temahuihot = new HotEntity { CombineAppId = "preference", OriginalAppName = "特卖汇", OpenType = "0", ImageUrl = "http://img.geilicdn.com/ec25bd2884138fc68ca8110df7b525f7.png" };
            hotList.Add(temahuihot);
            HotEntity shaonvzhuanghot = new HotEntity { CombineAppId = "Combine_Default", OriginalAppName = "少女装", OpenType = "0", ImageUrl = "http://img.geilicdn.com/bd1897d0e7dc4fa9e6977f36316db1b1.png" };
            hotList.Add(shaonvzhuanghot);
            HotEntity qingshunvhot = new HotEntity { CombineAppId = "Combine_058985b630953675f099ec4169eaae51", OriginalAppName = "轻熟女", OpenType = "0", ImageUrl = "http://img.geilicdn.com/2308eedf49fce5a997469f4ddea49644.png" };
            hotList.Add(qingshunvhot);

            hotList.ToObservableCollection<HotEntity>(HotItems);
        }

        protected override void OnInitialize()
        {
            GetHotInfo();
        }

        protected override void OnViewAttached(object view, object context)
        {
            base.OnViewAttached(view, context);
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
        }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);
        }
        #endregion

        #region Method
        private async void GetHotInfo()
        {
            BackData backData = await ServiceManager.GetHotInfo();
            if (backData.IsSuccess)
            {
                List<HotEntity> hotList = backData.ResposeData as List<HotEntity>;
                hotList.ToObservableCollection(this.HotItems);
            }
            else
            {
                Debug.WriteLine("msg:" + backData.Msg);
            }
        }
        #endregion

        #region ViewMoethod
        public void GoToDetail(HotEntity hotEntity)
        {
            Dictionary<string, string> subHotInfos = new Dictionary<string, string>();
            foreach (HotEntity item in hotEntity.SubCateories)
            {
                subHotInfos.Add(item.CombineAppId, item.OriginalAppName);
            }
            string subHotInfosStr = JsonConvert.SerializeObject(subHotInfos);
            _navigationService.UriFor<HotDetailPageViewModel>()
                .WithParam(x=>x.DisplayName,hotEntity.OriginalAppName)
                .WithParam(x=>x.CombineAppId,hotEntity.CombineAppId)
                .WithParam(x => x.SubHotInfosStr, subHotInfosStr).Navigate();
        }
        #endregion

    }
}
